package com.cj.image.mapper;

import com.cj.image.bean.ImageTagBean;
import com.cj.image.bean.TagInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TagMapper {
    @Select("select * from tag_info where tag_level = #{tag_level}")
    List<TagInfo> getTagInfoByLevel(String tag_level);

    @Select("select * from tag_info where tag_id in(select tag_id from image_tag where image_id=#{imageId}) ")
    List<TagInfo> getTagInfoByImageId(String imageId);
    @Select("select max(tag_id) as tag_id from tag_info where tag_level = #{tag_level} and parent_tag_id=#{parent_tag_id}")
    Long getMaxTagIdByLevel(String tag_level,String parent_tag_id);

    @Select("select max(tag_id) as tag_id from tag_info where tag_level = '1'")
    Long getMaxTagIdByLevel1();

    @Select("select * from tag_info where parent_tag_id = #{tag_id}")
    List<TagInfo> getTagInfoByTagId(String tag_id);

    @Insert("insert into tag_info(tag_id,tag_name,tag_level,parent_tag_id)value(#{tagId},#{tagName},#{tagLevel},#{parentTagId} )")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertTagInfo(TagInfo tagInfo);

    @Delete("DELETE  from tag_info where tag_id =#{tagId}")
    void deleteTagInfo(String tagId);

    @Select("SELECT      case when ti.tag_level = 1 then ti.tag_name      when ti.tag_level = 2 then concat(ti2.tag_name,'/',ti.tag_name )     when ti.tag_level = 3 then concat(ti3.tag_name,'/',ti2.tag_name,'/',ti.tag_name ) else ''      end as tag_name FROM      image.image_tag itg  left JOIN      image.tag_info ti ON itg.tag_id = ti.tag_id left JOIN      image.tag_info ti2 ON ti.parent_tag_id  = ti2.tag_id left JOIN      image.tag_info ti3 ON ti2.parent_tag_id  = ti3.tag_id    where itg.image_id=#{imageId}")
    List<String> getTagLine(String imageId);

    @Insert("insert into image_tag(image_id,tag_id)value(#{imageId},#{tagId} )")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertImageTag(ImageTagBean imagTag);

    @Delete("delete from image_tag where image_id=#{imageId} and tag_id=#{tagId} ")
    void deleteImageTag(ImageTagBean imagTag);
}
